package com.ics.sapecit.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

import com.ics.sapecit.bean.Column;
import com.ics.sapecit.bean.Table;
import com.ics.sapecit.dao.ISystemDao;
import com.ics.sapecit.service.ISystemService;

public class SystemServiceImpl implements ISystemService {
	private ISystemDao systemDao;
	private static Map<String, String> dataTypeMap = new HashMap<String, String>();

	static {
		dataTypeMap.put("CHAR", "java.lang.String");
		dataTypeMap.put("VARCHAR", "java.lang.String");
		dataTypeMap.put("VARCHAR2", "java.lang.String");
		dataTypeMap.put("LONG", "java.lang.Long");
		dataTypeMap.put("NUMBER", "java.lang.Long");
		dataTypeMap.put("DATE", "java.utils.Date");
		dataTypeMap.put("TIMESTAMP", "java.utils.Date");
		dataTypeMap.put("LONG RAW", "java.lang.Long");
		dataTypeMap.put("BLOB", "java.lang.Long");
		dataTypeMap.put("CLOB", "java.lang.String");
	}

	/**
	 * @return the systemDao
	 */
	public ISystemDao getSystemDao() {
		return systemDao;
	}

	/**
	 * @param systemDao
	 *            the systemDao to set
	 */
	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void createBeans() {
		List<Table> userTables = systemDao.getUserTables();
		for (Table table : userTables) {
			List<Column> columns = systemDao.getUserTablesColumns(table);
			Object obj = this.generateBean(columns);
			BeanMap.create(obj);
		}
	}

	private Object generateBean(List<Column> columns) {
		BeanGenerator generator = new BeanGenerator();
		System.out.println(dataTypeMap.get("NUMBER"));
		for (Column column : columns) {
			generator.addProperty(column.getColumnName(),
					dataTypeMap.get(column.getDataType()).getClass());
		}
		return generator.create();
	}
}
